package com.timothy.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import com.timothy.android.util.XMLUtil;

import com.timothy.question.BaseQuestion;
import com.timothy.question.BaseStation;
import com.timothy.config.Configuration;
import com.timothy.question.QuestionBuilder;

import android.app.Service;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

public class MessageReceiveService extends Service {
	public static final int MSG_REGISTER_CLIENT = 1;
	public static final int MSG_UNREGISTER_CLIENT = 2;
	public static final int MSG_QUESTION = 3;
	public static final int MSG_BASE_STATION = 4;
    public static final int MSG_SHORT_MESSAGE = 5;
	
    final Messenger rMessenger = new Messenger(new IncomingHandler());
    
	private static final int PORT = 9999;// 端口监听
	private static final String COMMAND_START = "<Message";
	private static final String COMMAND_END = "</Message>";
	
	List<Messenger> sMessengers = new ArrayList<Messenger>();
	
    @Override
    public IBinder onBind(Intent intent) {
    	return rMessenger.getBinder();
    }
    
    
	@Override
	public void onCreate() {
		Thread receiveThread = new ReceiveThread();
		receiveThread.start();
	}
	
	public void ShowDialog(String msg) {
		new AlertDialog.Builder(this).setTitle("提示").setMessage(msg)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					}
				}).show();
	}
	
	private void processCommand(String command){
		Object obj = null;
		int objType = 0;
	    String type = XMLUtil.getPropertyValue(command, "Type");
	    String content = XMLUtil.getChildren( command ).get(0);
		if( "CommonQuestion".equals( type ) ) {	
			BaseQuestion bq = QuestionBuilder.parseQuestion( content );
			bq.setXmlString(command);
			
			obj = bq;
			objType = MSG_QUESTION;
		} else if( "Background".equals( type ) ) {
			String value = XMLUtil.getChildren(content).get(0);
			
			Configuration.background = XMLUtil.getElementValue( value ); 
		} else if( "BackLight".equals( type ) ) {
			String value = XMLUtil.getChildren(content).get(0);
			
			Configuration.backLight =  Integer.valueOf( XMLUtil.getElementValue( value ) );	
		}  else if( "BubbleBackColor".equals( type ) ) {
			String value = XMLUtil.getChildren(content).get(0);
			
			Configuration.bubbleBackColor = XMLUtil.getElementValue( value );
		} else if( "ButtonBackColor".equals( type ) ) {
			String value = XMLUtil.getChildren(content).get(0);
			
			Configuration.buttonBackColor = XMLUtil.getElementValue( value );
		} else if( "BubbleFontColor".equals( type ) ) {
			String value = XMLUtil.getChildren(content).get(0);
			
			Configuration.bubbleFontColor = XMLUtil.getElementValue( value );
		} else if( "ButtonFontColor".equals( type ) ) {
			String value = XMLUtil.getChildren(content).get(0);
			
			Configuration.buttonFontColor = XMLUtil.getElementValue( value );
		} else if( "BubbleFontSize".equals( type ) ) {
			String value = XMLUtil.getChildren(content).get(0);
			
			Configuration.bubbleFontSize = Integer.valueOf( XMLUtil.getElementValue( value ) );
		} else if( "ButtonFontSize".equals( type) ) {
			String value = XMLUtil.getChildren(content).get(0);
			
			Configuration.buttonFontSize = Integer.valueOf( XMLUtil.getElementValue( value ) );
		} else if( "BaseStations".equals(type) ) {			
			obj = content;
			objType = MSG_BASE_STATION;
		} else if( "ShortMessage".equals(type) ) {
			obj = XMLUtil.getElementValue( XMLUtil.getChildren( content ).get(0) );
			objType = MSG_SHORT_MESSAGE;
		}
			
		if( obj != null ) {	
			for (int i=sMessengers.size()-1; i>=0; i--) {
	             try {
	            	 sMessengers.get(i).send( Message.obtain(null, objType, obj) );
	             } catch (RemoteException e) {
	            	 sMessengers.remove(i);
	             }
	        }
		}
	}
	 
	public class ReceiveThread extends Thread {
		//监听PC端消息
		public void run() {
			try {
				ServerSocket server = new ServerSocket(PORT);
				while (true) {
					Socket client = server.accept();
					BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
					StringBuffer commond = new StringBuffer();
					while (true) {
						String msg = in.readLine();
						if ( msg != null ) {
							msg = msg.trim();
							if( msg.startsWith( COMMAND_START ) ) {
								commond.setLength(0);
								commond.append( msg );
							} else if( COMMAND_END.equalsIgnoreCase(msg) ){
								commond.append( msg );
								break;
							} else {
								commond.append( msg );
							}
								
						} else {
							break;
						}
                        Log.i("MessageReceiveService",commond.toString());
					}
					in.close();
					client.close();
					
					processCommand( commond.toString() );
				}
			
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}		
	}
	
	class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_REGISTER_CLIENT:
                    sMessengers.add(msg.replyTo);
                    break;
                case MSG_UNREGISTER_CLIENT:
                    sMessengers.remove(msg.replyTo);
                    break;
            }
        }
    }
}
